package com.tiennt.android.apkextractor;

import java.io.File;
import java.util.List;

import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.Fragment;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.Loader;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.tiennt.android.apkextractor.loader.AppListLoader;
import com.tiennt.android.apkextractor.model.AppEntry;
import com.tiennt.android.apkextractor.model.AppListAdapter;
import com.tiennt.android.apkextractor.utilities.Config;
import com.tiennt.android.apkextractor.utilities.FileUtils;
import com.tiennt.android.apkextractor.utilities.ZipArchiveUtils;

public class AppListFragment extends Fragment implements
		LoaderCallbacks<List<AppEntry>> {

	private AppListAdapter mAdapter;
	private static final int LOADER_ID = 1;
	private ListView mListView;

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		mListView = (ListView) getView().findViewById(android.R.id.list);
		setHasOptionsMenu(true);

		mAdapter = new AppListAdapter(getActivity());
		mListView.setAdapter(mAdapter);
		mListView.setOnItemClickListener(onListItemClick);

		setListShown(false);

		if (Config.DEBUG) {
			Log.i(Config.TAG, "+++ Calling initLoader()! +++");
			if (getLoaderManager().getLoader(LOADER_ID) == null) {
				Log.i(Config.TAG, "+++ Initializing the new Loader... +++");
			} else {
				Log.i(Config.TAG,
						"+++ Reconnecting with existing Loader (id '1')... +++");
			}
		}

		// Initialize a Loader with id '1'. If the Loader with this id already
		// exists, then the LoaderManager will reuse the existing Loader.
		getLoaderManager().initLoader(LOADER_ID, null, this);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.app_list, container, false);
	}

	@Override
	public Loader<List<AppEntry>> onCreateLoader(int arg0, Bundle arg1) {
		if (Config.DEBUG)
			Log.i(Config.TAG, "+++ onCreateLoader() called! +++");
		return new AppListLoader(getActivity(), AppListLoader.AppType.USER);
	}

	@Override
	public void onLoadFinished(Loader<List<AppEntry>> loader,
			List<AppEntry> data) {
		if (Config.DEBUG)
			Log.i(Config.TAG, "+++ onLoadFinished() called! +++");
		mAdapter.setData(data);

		if (isResumed()) {
			setListShown(true);
		} else {
			setListShown(true);
		}
	}

	@Override
	public void onLoaderReset(Loader<List<AppEntry>> arg0) {
		// TODO Auto-generated method stub
		if (Config.DEBUG)
			Log.i(Config.TAG, "+++ onLoadReset() called! +++");
		mAdapter.setData(null);
	}

	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		inflater.inflate(R.menu.app_list_menu, menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {

		}
		return false;
	}

	private AdapterView.OnItemClickListener onListItemClick = new AdapterView.OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			// TODO Auto-generated method stub
			AppEntry entry = mAdapter.getItem(position);
			if (Config.DEBUG) {
				Log.v(Config.TAG, entry.getApkFile().getParent());
				Log.v(Config.TAG, entry.getApkFile().getName());
				FileUtils.copyFile(entry.getApkFile().getParent()
						+ File.separator, entry.getApkFile().getName(),
						Environment.getExternalStorageDirectory()
								+ File.separator);

				String unzipLocation = Environment
						.getExternalStorageDirectory() + "/unzipped/";

				ZipArchiveUtils.unZip(Environment.getExternalStorageDirectory()
						+ File.separator, entry.getApkFile().getName(),
						unzipLocation + entry.getApkFile().getName()
								+ File.separator);
				Toast.makeText(getActivity(), "Done", Toast.LENGTH_LONG).show();
			}
		}

	};

	public void setListShown(boolean showList) {
		if (showList) {
			mListView.setVisibility(View.VISIBLE);
		} else {
			mListView.setVisibility(View.GONE);
		}
	}

}
